# Create your models here.
from django.contrib.auth.models import User
from django.db import models


class Article(models.Model):
    category = models.CharField(max_length=100, verbose_name="文章分类", null=False)
    title = models.CharField(max_length=100, verbose_name="标题")
    author = models.CharField(max_length=50, verbose_name="作者")
    abstract = models.CharField(max_length=500, verbose_name="文章摘要", null=False)
    content = models.TextField(verbose_name="文章内容")
    c_time = models.DateTimeField(auto_now_add=True, verbose_name="创建日期")
    like_num = models.IntegerField(verbose_name="点赞数", default=0)
    reply_num = models.IntegerField(verbose_name="回复数", default=0)
    read_num = models.IntegerField(verbose_name="阅读数", default=0)
    against_num = models.IntegerField(verbose_name="反对数", default=0)
    user = models.ForeignKey(to=User, to_field="id", on_delete=models.CASCADE, null=True)

    class Meta:
        ordering = ("-c_time",)
        verbose_name_plural = "文章发布"
        verbose_name = "文章发布"

    def __str__(self):
        return self.title

class LogInfo(models.Model):
    types_dict_choice = [(0, "阅读"), (1, "点赞"), (2, "拍砖")]
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    types = models.IntegerField(verbose_name="分类", choices=types_dict_choice, default=0)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    c_time = models.DateTimeField(auto_now_add=True, verbose_name="创建日期")

    class Meta:
        verbose_name_plural = "点评日志"
        verbose_name = "点评日志"


class CommentInfo(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    c_time = models.DateTimeField(auto_now_add=True, verbose_name="创建日期")
    content = models.TextField(verbose_name="文章内容")

    class Meta:
        ordering = ("-c_time",)
        verbose_name_plural = "文章评论"
        verbose_name = "文章评论"

    def __str__(self):
        return self.content[:10]
